
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author cnavarro
 */
public class Recepteur extends Thread{

    //Données membres
    private int numPort;
    private Process process;
    private Tampon tamponReception;
    
    // Constructeur
    public Recepteur(Process _process, int numPort) {
        this.process = _process;
        this.numPort = numPort;
        this.tamponReception = this.process.getTamponDeReception();
    }
    
    @Override
    public void run ()
    {
        try
        {
            ServerSocket serverSocket = new ServerSocket (numPort);
            //try
            //{
            //}
                Socket socket = serverSocket.accept();
                ObjectInputStream fluxDEntree = new ObjectInputStream (socket.getInputStream ());            
                while (true)
                {
                        System.out.println("Process "+process.getNumProcess()+" Reception tourne");
                        try {
                            //try {
                            //Traitement
                            // Mise en mémoire tampon de réception
                       
                                Message message = (Message) fluxDEntree.readObject();
                                this.tamponReception.setElementTampon(message);
                           
                            //System.out.println("Process :"+process.getNumProcess()+" Taille du tampon de reception : " + process.getTamponDeReception().getTampon().size());
                            //} catch (ClassNotFoundException ex) {
                            //    Logger.getLogger(Recepteur.class.getName()).log(Level.SEVERE, null, ex);
                            //}
                            //Fermeture de la socket
                            //socket.close();
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(Recepteur.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    
                    
               }
            //}
            //catch (IOException e2)
            //{
            //    System.err.println(e2.getMessage());
            //}
        }
        catch (IOException e1)
        {
            System.err.println(e1.getMessage());
        }
    }
}
